package inputForm

import csv.CsvParserBibliography;
import csv.ObjFinder
import csv.CsvParserPathogen
import sugarbind_input.*


import java.util.*

class InputFormController {

	ObjFinder finder = new ObjFinder()
	CsvParserPathogen parserPathogen = new CsvParserPathogen()
	CsvParserBibliography parserBiblio = new CsvParserBibliography()
	
//	def article_TypeId
	def articleId
	def authorIds
//	def agent
	def lectinId
	def ligandId
	def sourceIds
	def binding_StrengthId
	def methodIds		
	def areaIds
	def diseaseIds
	
	
    def index() {
//		redirect(action:insert)
	}
	
	def update() {}
	
	def insert() {
				
//		article_TypeId = params.article_Type
		articleId = params.article
		authorIds = params.list('author')
		
//		def agent_Type
//		agent = params.agent
//		def lectin_Support
		lectinId = params.lectin
		
//		structure
		ligandId = params.ligand
		sourceIds = params.list('source')
				
		binding_StrengthId = params.binding_Strength
		methodIds = params.list('method')
			
		areaIds = params.list('area')
		diseaseIds	= params.list('disease')
			
//		printParams()
	
//		println(" insert param agent : $agent")
	}
	
	def save() {
		
		
//		println(parserPathogen)
		
		try
		{
		/*Mandatory values*/
		articleId = params.article
		lectinId = params.lectin
		ligandId = params.ligand

		/*Optional values*/
		authorIds = params.list('author')
		sourceIds = params.list('source')				
		binding_StrengthId = params.binding_Strength
		methodIds = params.list('method')			
		areaIds = params.list('area')
		diseaseIds	= params.list('disease')
		
		println(" save param : $params")
		printParams()
		println("========================")
		
		
		/*Get instances of article and authors if any and create the association*/
		Article article = Article.get(articleId)
		println(article)
		//TODO
		HashSet<Author> authors = Author.getAll(authorIds)
		println(authors)
		
		parserBiblio.csvParseAuthorArticle(authors, article)
//		article.save()
		
		/*Create the binding and associate methods, binding_strength and article*/
		
		Lectin lectin = Lectin.get(lectinId)
		println(lectin)
		Ligand ligand = Ligand.get(ligandId)
		println(ligand)
		
		HashSet<Source> sources = Source.getAll(sourceIds)
		ligand.setSources(sources)
		
		Binding_Strength binding_Strength = Binding_Strength.get(binding_StrengthId)
		println(binding_Strength)
		Database database = finder.findDatabase("SB")
		println(database)
		HashSet<Method> methods = Method.getAll(methodIds)
		println(methods)
				
		String bindingNotes = "" 
		Binding binding = parserPathogen.csvParseBinding(bindingNotes, database, ligand, binding_Strength, lectin, methods, article)
//		binding.save()
		println(binding)
		
		/*Create the agent-disease-area triplet if disease and area is not null*/
		Agent agent = lectin.lectin_Support.agent
		println(agent)
		ArrayList<Disease> diseases = Disease.getAll(diseaseIds)
		println(diseases)
		ArrayList<Area> areas = Area.getAll(areaIds)
		println(areas)
		def diseaseAreaAgents = parserPathogen.csvParseDiseaseAreaAgents(diseases, areas, agent)
		println(diseaseAreaAgents)
		
		
		ArrayList messages = new ArrayList()
		messages.add("Article : ${article} ")
		messages.add("Authors : ${authors} ")
		
		messages.add("___________________")
		
		messages.add("Binding : ${binding} ")
		messages.add("Lectin : ${lectin} ")
		messages.add("Ligand : ${ligand} ")
		messages.add("Ligand sources : ${sources} ")
		messages.add("Binding_Strength : ${binding_Strength}")
		messages.add("Methods : ${methods} ")
		
		messages.add("___________________")
		
		messages.add("Diseases : ${diseases} ")
		messages.add("Areas : ${areas} ")
		messages.add("Agent : ${agent} ")
		
		
		flash.message = messages
		
		
		}
		catch(Exception ex)
		{
			println(ex.getMessage())
			flash.message = ex.getMessage()
		}
		
	}
	
	
	
	
	def printParams()
	{
//		println("article_Type : $article_Type")
		println("article : $articleId")
		println("author : $authorIds")		
//		println("agent : $agent")
		println("lectin : $lectinId")
		println("ligand : $ligandId")
		println("source : $sourceIds")
		println("binding_Strength : $binding_StrengthId")
		println("method : $methodIds")
		println("area : $areaIds")
		println("disease : $diseaseIds")		
	}

}
